import axios from "axios";

const requestInstance = axios.create({
	baseURL: 'http://iot.hailitec.cn', // 正式
	// baseURL: 'http://125.46.49.120:8000', // 测试
	headers: {
		'Content-Type': 'application/x-www-form-urlencoded',
	}
})

// 添加请求拦截器
requestInstance.interceptors.request.use(function(config) {
	const token = uni.getStorageSync('token')
	if (token) config.headers.Authorization = token.startsWith(`Bearer`) ? token : `Bearer ${token}`
	return config;
}, function(error) {
	uni.showToast({
		title: '请求异常',
		icon: 'none'
	})
	return Promise.reject(error);
});

// 添加响应拦截器
requestInstance.interceptors.response.use(function(response) {
	return response.data;
}, function(error) {
	uni.showToast({
		title: '服务器异常',
		icon: 'none'
	})
	return Promise.reject(error);
});

export default requestInstance;